package deep8jvmdvm;

/*
synchronized

修饰代码块。
 */

import utils.PrintlnUtils;

public class TestSynchronized5 {

    private Object lock = new Object();

    public static void main(String[] args) {
        TestSynchronized5 testSynchronized1 = new TestSynchronized5();

        Thread thread1 = new Thread(new Runnable() {
            @Override
            public void run() {
                testSynchronized1.printLog();
            }
        });

        Thread thread2 = new Thread(new Runnable() {
            @Override
            public void run() {
                testSynchronized1.printLog();
            }
        });
        thread1.start();
        thread2.start();
    }

    public void printLog() {

        synchronized (lock) {
            for (int i = 0; i < 5; i++) {
                PrintlnUtils.println(Thread.currentThread().getName() + " is printing " + i);
            }
        }

    }

}
